<template>
  <el-dialog
    :title="!dataForm.grouporderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="团购表id" prop="groupbuyId">
      <el-input v-model="dataForm.groupbuyId" placeholder="团购表id"></el-input>
    </el-form-item>
    <el-form-item label="集合地址" prop="grouporderAddress">
      <el-input v-model="dataForm.grouporderAddress" placeholder="集合地址"></el-input>
    </el-form-item>
    <el-form-item label="车牌号" prop="grouporderPlatenum">
      <el-input v-model="dataForm.grouporderPlatenum" placeholder="车牌号"></el-input>
    </el-form-item>
    <el-form-item label="集合时间" prop="grouporderCollectiondate">
      <el-input v-model="dataForm.grouporderCollectiondate" placeholder="集合时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          grouporderId: 0,
          groupbuyId: '',
          grouporderAddress: '',
          grouporderPlatenum: '',
          grouporderCollectiondate: ''
        },
        dataRule: {
          groupbuyId: [
            { required: true, message: '团购表id不能为空', trigger: 'blur' }
          ],
          grouporderAddress: [
            { required: true, message: '集合地址不能为空', trigger: 'blur' }
          ],
          grouporderPlatenum: [
            { required: true, message: '车牌号不能为空', trigger: 'blur' }
          ],
          grouporderCollectiondate: [
            { required: true, message: '集合时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.grouporderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.grouporderId) {
            this.$http({
              url: this.$http.adornUrl(`/agent/grouporder/info/${this.dataForm.grouporderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.groupbuyId = data.grouporder.groupbuyId
                this.dataForm.grouporderAddress = data.grouporder.grouporderAddress
                this.dataForm.grouporderPlatenum = data.grouporder.grouporderPlatenum
                this.dataForm.grouporderCollectiondate = data.grouporder.grouporderCollectiondate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/agent/grouporder/${!this.dataForm.grouporderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'grouporderId': this.dataForm.grouporderId || undefined,
                'groupbuyId': this.dataForm.groupbuyId,
                'grouporderAddress': this.dataForm.grouporderAddress,
                'grouporderPlatenum': this.dataForm.grouporderPlatenum,
                'grouporderCollectiondate': this.dataForm.grouporderCollectiondate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
